<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>长理二手书交易管理平台</title>
    <meta name="description" content="长理二手书">
    <meta name="keywords" content="长理二手书">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <style>
        body {
            background: url('../img/微信图片_20240702151649.jpg') no-repeat center center fixed; /* 用你的背景图片路径替换这个 */
            background-size: cover;
        }

        .background-overlay {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(255, 255, 255, 0.5); /* 这里调整透明度 */
            z-index: -1;
        }

        .fNation {
            margin-left: 2px;
        }

        .fNationB {
            margin-left: 5px;
        }

        .content {
            position: relative;
            z-index: 1;
        }

        .book-p {
            width: 100%;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        /*.img-avatar{*/
        /*      width: 20px;*/
        /*      height: 20px;*/
        /*      display: inline-block;*/
        /*      background-position: center;*/
        /*      background-size: cover;*/
        /*      border-radius: 25px;*/
        /*      background-repeat: no-repeat;*/
        /*      transition:all 0.5s;*/
        /*  }*/
        /*.img-avatar:hover{*/
        /*    transform: translateX(100px);*/
        /*}*/

    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>二手书库<small>所有书籍</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>二手书库</el-breadcrumb-item>
            <el-breadcrumb-item>所有书籍</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="书籍编码/书名/ISBN号" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-select v-model="pagination.nationality" placeholder="请选择作者国籍" class="fNation">
                    <el-option label="中国" value="0"></el-option>
                    <el-option label="外籍华人" value="1"></el-option>
                    <el-option label="外籍" value="2"></el-option>
                </el-select>
                <el-button @click="handleCurrentChange(1)" class="dalfBut">查询书籍</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新增书籍</el-button>
            </div>
            <el-table class="tableContainer" size="small" current-row-key="id" :data="dataList" stripe
                      highlight-current-row>
                <el-table-column type="index" align="center" label="序号" width="50"></el-table-column>
                <el-table-column prop="book_image" label="书本图片" align="center">
                    <template slot-scope="scope">
                        <img :src="getBookImageUrl(scope.row.book_image)" alt="Book Image"
                             style="width: 50px; height: auto;">
                    </template>
                    <!--                                        <template slot-scope="scope">-->
                    <!--                                            <div class="img-avatar"-->
                    <!--                                                 :style="{backgroundImage:'url('+scope.row.book_image+')'}">-->
                    <!--                                            </div>-->
                    <!--                                        </template>-->
                </el-table-column>
                <el-table-column prop="name" label="书名" align="center"></el-table-column>
                <el-table-column prop="price" label="价格" align="center" width="60"></el-table-column>
                <el-table-column prop="number" label="库存数量" align="center" width="80"></el-table-column>
                <el-table-column prop="isbn" label="ISBN号" align="center"></el-table-column>
                <el-table-column prop="description" label="图书简介" align="center">
                    <template slot-scope="{ row }">
                        <el-popover placement="bottom" trigger="hover" :content="row.description">
                            <div slot="reference" class="book-p">{{ row.description }}</div>
                        </el-popover>
                    </template>
                </el-table-column>
                <el-table-column prop="notes" label="注意事项" align="center">
                    <template slot-scope="{ row }">
                        <el-popover placement="top" tigger="focus">
                            <p>{{row.notes}}</p>
                            <span slot="reference" :title="row.notes">
                                {{row.notes.length > 5? row.notes.slice(0,5) + '...' :row.notes }}
                            </span>
                        </el-popover>
                    </template>
                </el-table-column>
                <el-table-column prop="create_time" label="创建时间" align="center"></el-table-column>
                <el-table-column prop="update_time" label="更新时间" align="center"></el-table-column>
                <el-table-column label="操作" align="center" width="220">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button type="primary" size="mini" @click="showBuyBookDialog(scope.row)">购买</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增图书" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="书籍编码" prop="book_code">
                                    <el-input v-model="formData.book_code"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="书名" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="ISBN号" prop="isbn">
                                    <el-input v-model="formData.isbn"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="库存数量" prop="number">
                                    <el-input v-model="formData.number"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="作者国籍">
                                    <el-select v-model="formData.author_nationality">
                                        <el-option label="中国" value="0"></el-option>
                                        <el-option label="外籍华人" value="1"></el-option>
                                        <el-option label="外籍" value="2"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="上传图片">
                                    <el-upload
                                            class="avatar-uploader"
                                            action="http://localhost:8080/books/upload"
                                            :auto-upload="autoUpload"
                                            name="imgFile"
                                            :show-file-list="false"
                                            :on-success="handleAvatarSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="适用范围">
                                    <el-input v-model="formData.applicable_scope"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label=类型>
                                    <el-select v-model="formData.type">
                                        <el-option label="课外阅读书" value="1"></el-option>
                                        <el-option label="课内知识书" value="2"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="价格">
                                    <el-input v-model="formData.price"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="图书简介">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="注意事项">
                                    <el-input v-model="formData.notes" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑图书" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="书籍编码" prop="code">
                                    <el-input v-model="formData.book_code"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="书名" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="ISBN号" prop="isbn">
                                    <el-input v-model="formData.isbn"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="库存数量" prop="number">
                                    <el-input v-model="formData.number"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="作者国籍">
                                    <el-select v-model="formData.author_nationality">
                                        <el-option label="中国" value="0"></el-option>
                                        <el-option label="外籍华人" value="1"></el-option>
                                        <el-option label="外籍" value="2"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-row>
                                <el-col :span="24">
                                    <el-form-item label="编辑图片">
                                        <el-upload
                                                class="avatar-uploader"
                                                action="http://localhost:8080/books/upload"
                                                :auto-upload="autoUpload"
                                                name="imgFile"
                                                :show-file-list="false"
                                                :on-success="handleAvatarSuccess"
                                                :before-upload="beforeAvatarUpload">
                                            <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                        </el-upload>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-col :span="12">
                                <el-form-item label="适用范围">
                                    <el-input v-model="formData.applicable_scope"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="类型">
                                    <el-select v-model="formData.type">
                                        <el-option label="课外阅读书" value="1"></el-option>
                                        <el-option label="课内知识书" value="2"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="价格">
                                    <el-input v-model="formData.price"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="图书简介">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="注意事项">
                                    <el-input v-model="formData.notes" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 购买标签层-->

            <div class="add-form">
                <el-dialog title="购买图书（确认信息）" :visible.sync="dialogFormVisible7Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="书籍编码: " prop="code">
                                    <span>{{ formData.book_code}}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="书名: " prop="name">
                                    <span>{{formData.name}}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="ISBN号: " prop="isbn">
                                    <span>{{formData.isbn}}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="库存数量: " prop="number">
                                    <span>{{formData.number}}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="作者国籍: ">
                                    <span>{{ nationalityName }}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="适用范围: ">
                                    <span>{{formData.applicable_scope}}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="类型: ">
                                    <span>{{bookType}}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="价格: ">
                                    <span>{{formData.price}}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="图书简介: ">
                                    <span>{{formData.description}}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="注意事项: ">
                                    <span>{{formData.notes}}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="购买数量">
                                    <el-input v-model="purchaseQuantity" type="number" min="0"
                                              @input="calculateTotal"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="单价: ">
                                    <span>{{ unitPrice }}</span>
                                    <small class="discount-rules">(购买两本减少10元，购买三本以上打八折。)</small>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="成交金额: ">
                                    <span>{{ totalAmount }}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible7Edit = false">取消</el-button>
                        <el-button type="primary" @click="confirmPurchase()">确定</el-button>
                    </div>
                </el-dialog>

                <el-dialog title="查看购买信息" :visible.sync="dialogPurchaseInfoVisible">
                    <p>点击下方链接查看购买信息：</p>
                    <a href="transactions.html" @click.prevent="goToTransactionsPage">查看购买信息</a>
                    <span slot="footer" class="dialog-footer">
        <el-button @click="dialogPurchaseInfoVisible = false">关闭</el-button>
      </span>
                </el-dialog>
            </div>
        </div>
    </div>
    <div class="background-overlay"></div> <!-- 新增的背景覆盖层 -->
</div>

</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                nationality: '',
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 60,//总记录数
                queryString: null//查询条件
            },
            // bucketDomain: 'https://kuaikuai-1327428633.cos.ap-beijing.myqcloud.com/',
            autoUpload: true,
            imageUrl: null,
            unitPrice: 0,
            totalAmount: 0,
            transactionId: 0,
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            nationality: null,
            id: 0,
            purchaseQuantity: 1,
            dialogFormVisible: false,//增加表单是否可见
            dialogPurchaseInfoVisible: false,
            dialogFormVisible4Edit: false,//编辑表单是否可见
            dialogFormVisible7Edit: false,
            rules: {//校验规则
                code: [{required: true, message: '项目编码为必填项', trigger: 'blur'}],
                name: [{required: true, message: '项目名称为必填项', trigger: 'blur'}]
            }
        },
        computed: {
            calculateTotal() {
                this.unitPrice = this.formData.price;
                if (this.purchaseQuantity < 2) {
                    this.totalAmount = this.unitPrice * this.purchaseQuantity;
                } else if (this.purchaseQuantity == 2) {
                    this.totalAmount = this.unitPrice * this.purchaseQuantity - 10;
                } else {
                    this.totalAmount = this.unitPrice * this.purchaseQuantity * 0.8;
                }

            },
            nationalityName() {
                const nationalityMap = {
                    0: '中国',
                    1: '外籍华人',
                    2: '外籍'
                };
                return nationalityMap[this.formData.author_nationality] || '未知国籍';
            },
            bookType() {
                const bookTypeMap = {
                    1: '课外阅读书',
                    2: '课内知识书'
                };
                return bookTypeMap[this.formData.type] || '未知国籍';
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            confirmPurchase(){

                this.buyBook()
               // this.showPurchaseInfo()
            },

            showPurchaseInfo() {
                // 显示购买信息弹窗
                this.dialogPurchaseInfoVisible = true;
            },

            goToTransactionsPage() {
                // 跳转到transactions.html页面，并自动打开弹窗
                window.location.href = `transactions.html?transactionId=${this.transactionId}&autoOpen=true`;
            },

            getBookImageUrl(filename) {
                return '/upload/' + filename
                //return this.bucketDomain + filename;
            },

            handleAvatarSuccess(response, file) {
                if (response.flag) {
                    console.log(23456,response.data)
                    // this.imageUrl = "https://kuaikuai-1327428633.cos.ap-beijing.myqcloud.com/" + response.data
                    this.imageUrl = this.getBookImageUrl(response.data);
                    this.$message.success(response.msg)
                    console.log(1090,response)
                    this.formData.book_image = response.data
                } else {
                    this.$message.error(response.msg)
                }
            },
            //上传图片之前执行
            beforeAvatarUpload(file) {
                // const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;
                // if (!isJPG) {
                // this.$message.error('上传图片只能是 JPG 格式!');
                // }
                if (!isLt2M) {
                    this.$message.error('上传图片大小不能超过 2MB!');
                }
                return isLt2M;
            },
            showBuyBookDialog(row) {
                this.dialogFormVisible7Edit = true
                axios.get("http://localhost:8080/books/one?id=" + row.id).then(resp => {
                    console.log(resp)
                    this.formData = resp.data.data;
                })
            },
            buyBook() {
                const params1 = {
                    id: this.formData.id,
                    purchaseQuantity: this.purchaseQuantity
                };

                axios.post('http://localhost:8080/books/buyBook', params1)
                    .then((response) => {
                        if (response.data.flag) {
                            this.$message({
                                type: 'success',
                                message: response.data.msg
                            });
                            this.findPage();
                            this.dialogFormVisible7Edit = false;
                        } else {
                            this.handleError(response.data.msg);
                        }
                    })
                    .catch((error) => {
                        this.handleError(error.message);
                    });

                const params2 = {
                    book_id: this.formData.id,
                    purchaseQuantity: this.purchaseQuantity,
                    totalAmount: this.totalAmount,
                };
                axios.post('http://localhost:8080/books/transaction', params2)
                    .then((resp) => {
                        console.log(2020, resp);
                        this.transactionId = resp.data.data;
                        console.log(2013, this.transactionId);
                        this.showPurchaseInfo(); // 显示购买信息弹窗
                    })
                    .catch((error) => {
                        this.handleError(error.message);
                    });
            },

            handleError(errorMessage) {
                this.$message({
                    type: 'error',
                    message: errorMessage || 'An unknown error occurred.'
                });
            },

            handleEdit() {
                axios.post('http://localhost:8080/books/update', this.formData).then(resp => {
                    console.log(resp)
                    if (resp.data.flag) {
                        this.$message({
                            type: 'success',
                            message: resp.data.msg
                        })
                        this.pagination.currentPage = 1;

                        this.findPage();

                        this.dialogFormVisible4Edit = false
                    }
                })
            },
            //添加
            handleAdd() {
                axios.post('http://localhost:8080/books/create', this.formData).then(resp => {
                    console.log(222,resp)
                    if (resp.data.flag) {
                        this.$message({
                            type: 'success',
                            message: resp.data.msg
                        })
                        this.findPage()
                        this.dialogFormVisible = false
                    }
                })

            },
            //分页查询
            findPage() {
                axios.post("http://localhost:8080/books/page", this.pagination).then(resp => {
                    console.log(112345, resp)
                    this.dataList = resp.data.data.list;
                    this.pagination.total = resp.data.data.total;
                    console.log(1234321,this.pagination)
                })
            },
            // 重置表单
            resetForm() {
            },
            // 弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true

                this.formData = {}
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                this.dialogFormVisible4Edit = true
                axios.get("http://localhost:8080/books/one?id=" + row.id).then(resp => {
                    console.log(resp)
                    this.formData = resp.data.data;
                    this.imageUrl = this.getBookImageUrl(row.book_image);
                    })

            },
            //切换页码
            handleCurrentChange(currentPage) {
                console.log(currentPage)
                this.pagination.currentPage = currentPage
                this.findPage()
            },
            // 删除
            handleDelete(row) {
                this.$confirm('此操作将永久删除该书目, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                }).then(() => {
                    // 发送ajax请求，执行删除
                    axios.get("http://localhost:8080/books/del?id=" + row.id).then(resp => {
                        if (resp.data.flag) {
                            this.$message({
                                type: 'success',
                                message: resp.data.msg
                            })
                            this.findPage()
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
        }
    })
</script>
</html>
